<?php

class Demoweibo_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getStatus($uid, $module) {
        $status = array();
        $query = $this->db->query("Select * from wb_demo_status where uid=" . $uid . " and module='" . $module . "'");
        foreach ($query->result() as $row) {
            $s = $this->constuctStatus($row);
            $status[] = $s;
        }
        foreach($status as $key=>$row){
            $score[$key]=$row["score"];
        }
        array_multisort($score,SORT_DESC,$status);
        return $status;
    }

    public function constuctStatus($row) {
        $s = array();
        $s["score"]=$row->score;
        $s["mid"] = $row->mid;
        $s["fname"] = $this->db->query("select username from wb_demo_user where uid=" . $row->fid)->first_row()->username;
        $s["text"] = $row->text;
        $s["spic"] = $row->thumbnail_pic;
        if ($row->retweetID != null) {
            $s["retweetID"] = $row->retweetID;
            $s["retweetUID"] = $row->retweetUID;
            $nameQuery = $this->db->query("select username from wb_demo_user where uid=" . $row->retweetUID);
            $s["retweetUsername"]=$row->retweetUID;
            if ($nameQuery->num_rows() > 0) {
                $s["retweetUsername"] = $nameQuery->first_row()->username;
            }
            $s["retweetText"]=$row->retweetText;
            $s["retweetSPIC"]=$row->retweetSPIC;
        }
        return $s;
    }

}

?>
